package cn.tannn.ops.customer.entity;


import cn.tannn.ops.common.pojo.JpaCommonBean;
import cn.tannn.ops.customer.constant.MenuStatus;
import cn.tannn.ops.customer.constant.MenuType;
import cn.tannn.ops.customer.controller.dto.MenuAlter;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Index;
import jakarta.persistence.Table;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.annotations.Comment;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;

/**
 * 菜单路由
 *
 * @author tnnn
 * @version V1.0
 * @date 2022-11-7
 */
@Entity
@Table(name = "tb_menu", indexes = {
        @Index(name = "code_index", columnList = "code", unique = true),
})
@Comment("菜单路由")
@Getter
@Setter
@ToString
@RequiredArgsConstructor
@DynamicUpdate
@DynamicInsert
public class Menu extends JpaCommonBean<Menu> {



    /**
     * code
     */
    @Comment("code")
    @Column(columnDefinition = "varchar(50)  not null")
    private String code;


    /**
     * 菜单名;同级名称唯一
     */
    @Comment("菜单名[同级名称唯一]")
    @Column(columnDefinition = "varchar(100)  not null")
    private String name;


    /**
     * 父级code
     */
    @Comment("父级code")
    @Column(columnDefinition = "varchar(50)  not null")
    private String parentCode;

    /**
     * 父级名称
     */
    @Comment("父级名称")
    @Column(columnDefinition = "varchar(100)  not null")
    private String parentName;


    /**
     * 排序[数字越小排在越前面]
     */
    @Comment("排序[数字越小排在越前面]")
    @Column(columnDefinition = "int default 99")
    private Integer sort;


    /**
     * 菜单组件
     */
    @Comment("菜单组件")
    @Column(columnDefinition = "varchar(255) ")
    private String component;


    /**
     * 菜单组件名
     */
    @Comment("菜单组件名")
    @Column(columnDefinition = "varchar(255) ")
    private String componentName;


    /**
     * 菜单图标
     */
    @Comment("菜单图标")
    @Column(columnDefinition = "varchar(255)")
    private String icon;


    /**
     * 菜单路由
     */
    @Comment("菜单路由")
    @Column(columnDefinition = "varchar(255)")
    private String router;


    /**
     * 类型;0、节点；1、功能；2、按钮
     *
     * @see MenuType
     */
    @Comment("菜单类型;0、节点；1、功能；2、按钮")
    @Column(columnDefinition = "int default 0")
    private Integer type;


    /**
     * 菜单状态;0、禁用；1、启用；默认启用
     *
     * @see MenuStatus
     */
    @Comment("菜单状态;0、禁用；1、启用；默认启用")
    @Column(columnDefinition = "int default 1")
    private Integer status;


    public static Menu top() {
        Menu top = new Menu();
        top.setCode("menu");
        top.setName("顶级菜单");
        return top;
    }


    public Menu alterMenuToMenu(MenuAlter alter) {

        if (StringUtils.isNotBlank(alter.getCode())) {
            this.code = alter.getCode();
        }

        if (StringUtils.isNotBlank(alter.getName())) {
            this.name = alter.getName();
        }

        if (alter.getType() != null) {
            this.type = alter.getType();
        }

        if (StringUtils.isNotBlank(alter.getRouter())) {
            this.router = alter.getRouter();
        }

        if (StringUtils.isNotBlank(alter.getParentCode())) {
            this.parentCode = alter.getParentCode();
        }

        if (StringUtils.isNotBlank(alter.getParentName())) {
            this.parentName = alter.getParentName();
        }

        if (alter.getSort() != null) {
            this.sort = alter.getSort();
        }

        if (alter.getIcon() != null) {
            this.icon = alter.getIcon();
        }

        if (alter.getStatus() != null) {
            this.status = alter.getStatus();
        }

        if (StringUtils.isNotBlank(alter.getComponent())) {
            this.component = alter.getComponent();
        }

        if (StringUtils.isNotBlank(alter.getComponentName())) {
            this.componentName = alter.getComponentName();
        }
        return this;
    }



}
